package ewms.controller;

import java.util.List;
import java.util.Arrays;
import java.util.stream.Collectors;

import ewms.bo.EwmsTransferWarehouseAddBo;
import ewms.bo.EwmsTransferWarehouseEditBo;
import ewms.bo.EwmsTransferWarehouseQueryBo;
import ewms.bo.EwmsOutWarehouseQueryBo;
import ewms.service.IEwmsOutWarehouseService;
import ewms.vo.EwmsOutWarehouseVo;
import ewms.vo.transfer_warehouse.EwmsTransferWarehouseWrapperVo;
import ewms.vo.transfer_warehouse.EwmsTransferWarehouseVo;
import ewms.service.IEwmsTransferWarehouseService;
import lombok.RequiredArgsConstructor;
import javax.validation.constraints.*;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import ewms.common.annotation.RepeatSubmit;
import ewms.common.annotation.Log;
import ewms.common.core.controller.BaseController;
import ewms.common.core.domain.AjaxResult;
import ewms.common.enums.BusinessType;
import ewms.common.utils.poi.ExcelUtil;
import ewms.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 危废转库Controller
 *
 * @author zhaoxiaochun
 * @date 2022-02-15
 */
@Api(value = "危废转库控制器", tags = {"危废转库管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/business/transferWarehouse")
public class EwmsTransferWarehouseController extends BaseController {

    private final IEwmsTransferWarehouseService iEwmsTransferWarehouseService;
    private final IEwmsOutWarehouseService iEwmsOutWarehouseService;

	/**
	 * 查询危废可转库列表
	 * @return
	 */
	@ApiOperation("查询危废可转库列表")
	@PreAuthorize("@ss.hasPermi('business:transferWarehouse:list')")
	@GetMapping("/preTransferList")
	public TableDataInfo<EwmsOutWarehouseVo> preTransferList(@Validated EwmsOutWarehouseQueryBo bo) {
		return iEwmsOutWarehouseService.selectPagePreOutWarehouseList(bo);
	}

	/**
	 * 导出危废可转库列表
	 */
	@ApiOperation("导出危废可转库列表")
	@PreAuthorize("@ss.hasPermi('business:transferWarehouse:export')")
	@Log(title = "危废转库", businessType = BusinessType.EXPORT)
	@GetMapping("/preTransferExport")
	public AjaxResult<EwmsOutWarehouseVo> preTransferExport(@Validated EwmsOutWarehouseQueryBo bo) {
		List<EwmsOutWarehouseVo> list = iEwmsOutWarehouseService.selectPreOutWarehouseList(bo);
		ExcelUtil<EwmsOutWarehouseVo> util = new ExcelUtil<EwmsOutWarehouseVo>(EwmsOutWarehouseVo.class);
		return util.exportExcel(list, "危废转库");
	}

    /**
     * 查询危废转库列表
     */
    @ApiOperation("查询危废转库列表")
    @PreAuthorize("@ss.hasPermi('business:transferWarehouse:list')")
    @GetMapping("/list")
    public TableDataInfo<EwmsTransferWarehouseWrapperVo> list(@Validated EwmsTransferWarehouseQueryBo bo) {
        return iEwmsTransferWarehouseService.selectPageTransferWarehouseList(bo);
    }

    /**
     * 导出危废转库列表
     */
    @ApiOperation("导出危废转库列表")
    @PreAuthorize("@ss.hasPermi('business:transferWarehouse:export')")
    @Log(title = "危废转库", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult<EwmsTransferWarehouseWrapperVo> export(@Validated EwmsTransferWarehouseQueryBo bo) {
        List<EwmsTransferWarehouseWrapperVo> list = iEwmsTransferWarehouseService.selectTransferWarehouseList(bo);
        list = list.stream().peek(EwmsTransferWarehouseWrapperVo::convertTransferRecord).collect(Collectors.toList());
        ExcelUtil<EwmsTransferWarehouseWrapperVo> util = new ExcelUtil<EwmsTransferWarehouseWrapperVo>(EwmsTransferWarehouseWrapperVo.class);
        return util.exportExcel(list, "危废转库记录");
    }

    /**
     * 获取危废转库详细信息
     */
    @ApiOperation("获取危废转库详细信息")
    @PreAuthorize("@ss.hasPermi('business:transferWarehouse:query')")
    @GetMapping("/{transferWarehouseId}")
    public AjaxResult<EwmsTransferWarehouseVo> getInfo(@NotNull(message = "主键不能为空")
                                                  @PathVariable("transferWarehouseId") Long transferWarehouseId) {
        return AjaxResult.success(iEwmsTransferWarehouseService.queryById(transferWarehouseId));
    }

    /**
     * 危废转库
     */
    @ApiOperation("危废转库")
    @PreAuthorize("@ss.hasPermi('business:transferWarehouse:add')")
    @Log(title = "危废转库", businessType = BusinessType.INSERT)
    @RepeatSubmit
    @PostMapping()
    public AjaxResult<Void> add(@Validated @RequestBody EwmsTransferWarehouseAddBo bo) {
        return toAjax(iEwmsTransferWarehouseService.insertByAddBo(bo) ? 1 : 0);
    }

    /**
     * 修改危废转库
     */
    @ApiOperation("修改危废转库")
    @PreAuthorize("@ss.hasPermi('business:transferWarehouse:edit')")
    @Log(title = "危废转库", businessType = BusinessType.UPDATE)
    @RepeatSubmit
    @PutMapping()
    public AjaxResult<Void> edit(@Validated @RequestBody EwmsTransferWarehouseEditBo bo) {
        return toAjax(iEwmsTransferWarehouseService.updateByEditBo(bo) ? 1 : 0);
    }

    /**
     * 删除危废转库
     */
    @ApiOperation("删除危废转库")
    @PreAuthorize("@ss.hasPermi('business:transferWarehouse:remove')")
    @Log(title = "危废转库" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{transferWarehouseIds}")
    public AjaxResult<Void> remove(@NotEmpty(message = "主键不能为空")
                                       @PathVariable Long[] transferWarehouseIds) {
        return toAjax(iEwmsTransferWarehouseService.deleteWithValidByIds(Arrays.asList(transferWarehouseIds), true) ? 1 : 0);
    }
}
